const loadpromotions=require('./promotions.js')
const item =require('./items.js')
//console.log(item());
//console.log(loadpromotions());
let loadAllItems = item();
let promotion= loadpromotions();
function GetcartItems(inputs){
  let cartItems=[];
  let result={};
for(let i of inputs)
{
  if(!result[i])
  {
    let arr=i.split(" x ") ;
       result[i]={};
       result[i].id=arr[0];
       result[i].count=arr[1];
       for(let j of loadAllItems)
       {
         //console.log(j.name);
         if(j.id == result[i].id)
         {
           result[i].name = j.name;
           //console.log(j.name); 
           result[i].price=j.price;
          }
       }
  }
}
for(let i in result)
cartItems.push(result[i]);
    // console.log(cartItems); 
     return cartItems;
}
    

//采取优惠方式
    function Discount(cartItems)
    {
      let halfsaved=0;
      let fillsaved=0;
      for(let i of cartItems)
      {
        for(let j of promotion[1].items)
        {
          if(i.id==j)
          {
             halfsaved+=(i.price*i.count)/2;
          }
        }
      }
      //console.log( halfsaved)
      let total=cartItems.reduce(function(sum,value){
      return sum+(value.price*value.count);
      },0);
      //console.log( total)
      if(total>30)
      {
         fillsaved+=6;
      }
      //console.log(fillsaved);
      let usediscount={};
      if(halfsaved==fillsaved)
      {
        usediscount.type='无优惠';
        usediscount.saved=0;
       // console.log(usediscount);
      }
      else if(fillsaved>=halfsaved&&fillsaved!==0&&halfsaved!==0)
      {
        usediscount.type=promotion[0].type;
        usediscount.saved=fillsaved;
       //return usediscount;
       //console.log(usediscount);
      }
      else if(fillsaved<halfsaved)
      {
        usediscount.type=promotion[1].type;
        usediscount.saved=halfsaved;
        //console.log(usediscount);
      }//console.log(usediscount);
     
      return usediscount;
    }
    function Getsummary(cartItems,usediscount)
    {
      let total1=cartItems.reduce(function(sum,value){
        return sum+(value.price*value.count);
        },0);
      let summary=total1-usediscount.saved;
      console.log(summary);
      return summary;
    }



   // let inputs = ["ITEM0013 x 4"];
module.exports =
 function bestCharge(inputs) {
  
 
//let inputs = ["ITEM0001 x 1", "ITEM0013 x 2", "ITEM0022 x 1"]; 
let cartItems = GetcartItems(inputs);
let discount = Discount(cartItems);
let subtotal = Getsummary(cartItems,discount);
result= 
'============= 订餐明细 =============\n';
if(discount.type=='指定菜品半价')
{
 // console.log(discount)
  cartItems.forEach(function(cartItems){
    result+=cartItems.name+' x '+cartItems.count+' = '+(cartItems.count*cartItems.price)+'元\n';
  });
    result+='-----------------------------------\n' ;
    result+='使用优惠:'+'\n'+discount.type+'(黄焖鸡，凉皮)'+'，省'+discount.saved+'元\n';
    result+='-----------------------------------\n';
    result+='总计：'+subtotal+'元\n'+'===================================\n';
}
else if(discount.type=='满30减6元')
{
  //console.log(discount)
    cartItems.forEach(function(cartItems){
      result+=cartItems.name+' x '+cartItems.count+' = '+(cartItems.count*cartItems.price)+'元\n';
    });
      result+='-----------------------------------\n' ;
      result+='使用优惠:'+'\n'+discount.type+'，省'+discount.saved+'元\n';
      result+='-----------------------------------\n';
      result+='总计：'+subtotal+'元\n'+'===================================\n';
}
else 
{

  console.log(discount)
    cartItems.forEach(function(cartItems){
      result+=cartItems.name+' x '+cartItems.count+' = '+(cartItems.count*cartItems.price)+'元\n';
    });
      result+='-----------------------------------\n';
      result+='总计：'+subtotal+'元\n'+'===================================\n';
    }
 // console.log(result);
  return result;
}
//console.log(bestCharge(inputs)) 